/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

#include "vmsys.h"
#include "vmio.h"
#include "vmgraph.h"
#include "vmchset.h"
#include "vmstdlib.h"
#include "ILAppBase.h"
#include "ILControls/ILControlCollect.h"
/* ---------------------------------------------------------------------------
* global variables
* ------------------------------------------------------------------------ */

/* ---------------------------------------------------------------------------
 * local variables
 * ------------------------------------------------------------------------ */
/*
* system events 
*/
void handle_sysevt(ILINT message, ILINT param);

/*
* key events 
*/
void handle_keyevt(ILINT event, ILINT keycode);

/*
* pen events
*/
void handle_penevt(ILINT event, ILINT x, ILINT y);

/*
* demo
*/
static void draw_hello(void);

void demo_init_instance(void)
{
	DEMO_APP* app_ptr = il_get_app_instance();

	app_ptr->lcd_x = 0;
	app_ptr->lcd_y = 0;
	app_ptr->lcd_w = vm_graphic_get_screen_width();
	app_ptr->lcd_h = vm_graphic_get_screen_height();
}
/*
* entry
*/
void vm_main(void) {
	/* initialize layer handle */
	s_demo_app.layer_hdl[0] = -1;	
	
	/* register system events handler */
	vm_reg_sysevt_callback(handle_sysevt);
	
	/* register keyboard events handler */
	vm_reg_keyboard_callback(handle_keyevt);
	
	/* register pen events handler */
	vm_reg_pen_callback(handle_penevt);
}

void handle_sysevt(ILINT message, ILINT param) {
	switch (message) {
	case VM_MSG_CREATE:	/* the message of creation of application */
	case VM_MSG_ACTIVE: /* the message of application state from inactive to active */
		/*cerate base layer that has same size as the screen*/
		s_demo_app.layer_hdl[0] = vm_graphic_create_layer(0, 0, 
			vm_graphic_get_screen_width(),		/* get screen width */
			vm_graphic_get_screen_height(),		/* get screen height */
			-1);		/* -1 means layer or canvas have no transparent color */
		
		/* set clip area*/
		vm_graphic_set_clip(0, 0, 
			vm_graphic_get_screen_width(), 
			vm_graphic_get_screen_height());
		il_reset_layer_clip(s_demo_app.layer_hdl[0]);
		break;
		
	case VM_MSG_PAINT:	/* the message of asking for application to repaint screen */
		draw_hello();	/* draw hello world! */
		break;
		
	case VM_MSG_INACTIVE:	/* the message of application state from active to inactive */
	case VM_MSG_QUIT:		/* the message of quit application */
		if( s_demo_app.layer_hdl[0] != -1 )
			vm_graphic_delete_layer(s_demo_app.layer_hdl[0]);
		
		break;	
	}
}

void handle_keyevt(ILINT event, ILINT keycode) {
	/* press any key and return*/
	if( s_demo_app.layer_hdl[0] != -1 )
	{
		vm_graphic_delete_layer(s_demo_app.layer_hdl[0]);
		s_demo_app.layer_hdl[0] = -1;
	}
	
	vm_exit_app();		/* quit application */
}

void handle_penevt(ILINT event, ILINT x, ILINT y)
{
	/* touch and return*/
	if( s_demo_app.layer_hdl[0] != -1 )
	{
		vm_graphic_delete_layer(s_demo_app.layer_hdl[0]);
		s_demo_app.layer_hdl[0] = -1;
	}
	
	vm_exit_app();		/* quit application */
}

static void draw_hello(void) 
{
	ILUINT8* layer_buffer;
	il_image* pImage; 
	il_label* pLabel;
	il_button* pButton;
	il_rect rect;
	
	rect.x = 50;
	rect.y = 50;
	rect.w = 108;
	rect.h = 59;
	layer_buffer = vm_graphic_get_layer_buffer(s_demo_app.layer_hdl[0]);
	
	vm_graphic_fill_rect(layer_buffer, 0, 0, vm_graphic_get_screen_width(), vm_graphic_get_screen_height(), VM_COLOR_BLACK, VM_COLOR_WHITE);

	//pImage = il_ctrl_image_create(layer_buffer, &rect, IL_IMAGE_FROM_FILE, L"E:\\MRElogo.gif", FALSE);
	//pImage->set_show_type((il_control*)pImage, IL_IMAGE_TILED);
	//pImage->set_alpha((il_control*)pImage, 100);
	//pImage->base.paint_hdlr((il_control*)pImage);

	//pLabel = il_ctrl_label_create(layer_buffer, rect, L"Hello, Luliang!");
	//pLabel->base.paint_hdlr((il_control*)pLabel);

	il_flush();
	//il_ctrl_image_destroy(pImage);
}

